// by Xeno

sleep 1.012;

X_XMarkerVehicles = {
	_rem = [];
	{
		if (!isNull _x) then {
			_m = _x getVariable "d_marker";
			_m setMarkerPosLocal (getPosASL _x);
			// This variable will prevent resetting the color and text on every run of the loop, to save on processing power.
			_m_status = _x getVariable "d_marker_status";
			if (isNil "_m_status") then { _x setVariable ["d_marker_status",true]; _m_status = false;};
			if (d_v_marker_dirs) then {_m setMarkerDirLocal (direction _x)};
			if ((damage _x >= 1) and _m_status) then {
				_m setMarkerColorLocal "ColorRed"; _m setMarkerTextLocal "Destroyed";
				_x setVariable ["d_marker_status",false];
			};
			if ((damage _x <= 0.9) and (!_m_status)) then {
				_m setMarkerColorLocal (_x getVariable "d_marker_color"); _m setMarkerTextLocal (_x getVariable "d_marker_text"); 
				_x setVariable ["d_marker_status",true];
			};
		};
		if (isNull _x || !alive _x) then {_rem set [count _rem, _x]};
		sleep 0.03;
	} forEach d_marker_vecs;
	if (count _rem > 0) then {d_marker_vecs = d_marker_vecs - _rem};
};

X_XMarkerPlayers = {
	private ["_i","_ap","_as"];
	_i = 0;
	if (d_show_player_marker > 0) then {
		{
			_ap = missionNamespace getVariable _x;
			_as = _x;
			if (!isNil "_ap") then {
				if (!isnull _ap) then {
					if( alive _ap && isPlayer _ap) then {
						_as setMarkerAlphaLocal 1;
						_as setMarkerPosLocal getPosASL _ap;
						_as setMarkerTextLocal (switch (d_show_player_marker) do {
							case 1: {name _ap};
							case 2: {""};
							case 3: {d_player_roles select _i};
							case 4: {localize "MSG_DOM_MRKHEALTH" + " " + str(9 - round(9 * damage _ap))};
							default {""};
						});
						if (! isNil("d_p_markerSize")) then {	_as setMarkerSizeLocal d_p_markerSize;};
						if (d_p_marker_dirs) then {_as setMarkerDirLocal (direction (vehicle _ap))};
					};
				};
			} else {
				_as setMarkerPosLocal [0,0];
				_as setMarkerTextLocal "";
				_as setMarkerAlphaLocal 0;
			};
			_i = _i + 1;
			sleep 0.03;
		} forEach d_player_entities;
	};
};

if (!d_dont_show_player_markers_at_all) then {
	_mtyp = d_p_marker;
	{
		[_x, [0,0],"ICON","ColorGreen",[0.4,0.4],"",0,_mtyp] call XfCreateMarkerLocal;
		sleep 0.01;
	} forEach d_player_entities;
};

for "_i" from 0 to (count d_player_entities) - 1 do {
	g_oVariableStorage setVariable [(d_player_entities select _i), [_i, d_player_roles select _i]];
};

sleep 0.01;
